package com.bp.hearthealth.pulse.lnapps.appsflyer

import com.appsflyer.AppsFlyerConversionListener
import com.appsflyer.AppsFlyerLib
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.BuildConfig
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import java.util.Objects

object AppsFlyer {

    fun init() {
        AppsFlyerLib.getInstance()
            .init(Constants.FLYERLIB_KEY, conversionListener, BpApplication.context)
        AppsFlyerLib.getInstance().start(BpApplication.context)
        if (BuildConfig.DEBUG) {
            AppsFlyerLib.getInstance().setDebugLog(true)
        }
        LogUtil.i("初始化AppsFlyer")
    }

    private var conversionListener: AppsFlyerConversionListener = object : AppsFlyerConversionListener {
        override fun onConversionDataSuccess(conversionDataMap: Map<String, Any>) {
            listener?.onConversionDataSuccess(conversionDataMap)
//            LogUtil.i( "AppsFlyer: conversionDataMap: $conversionDataMap")
            for (attrName in conversionDataMap.keys) {
                LogUtil.i(
                    "AppsFlyer: Conversion attribute: " + attrName + " = " + conversionDataMap[attrName]
                )
            }
            val status: String = Objects.requireNonNull(conversionDataMap["af_status"]).toString()
            if (status == "Non-organic") {

                LogUtil.i( "AppsFlyer: media_source: ${conversionDataMap["media_source"]}")
                LogUtil.i( "AppsFlyer: af_channel: ${conversionDataMap["af_channel"]}")
                if (Objects.requireNonNull(conversionDataMap["is_first_launch"]).toString()
                        .equals("true")
                ) {
                    LogUtil.i("AppsFlyer: Conversion: First Launch")
                } else {
                    LogUtil.i("AppsFlyer: Conversion: Not First Launch")
                }
            } else {
                LogUtil.i("AppsFlyer: Conversion: This is an organic install.")
            }
            val conversionData = conversionDataMap
        }

        override fun onConversionDataFail(errorMessage: String) {
            listener?.onConversionDataFail(errorMessage)
            LogUtil.i("AppsFlyer: error getting conversion data: $errorMessage")
        }

        override fun onAppOpenAttribution(attributionData: Map<String, String>) {
            LogUtil.i("AppsFlyer: onAppOpenAttribution: This is fake call.")
        }

        override fun onAttributionFailure(errorMessage: String) {
            listener?.onAttributionFailure(errorMessage)
            LogUtil.i("AppsFlyer: error onAttributionFailure : $errorMessage")
        }
    }

    interface AfConversionListener {
        //转换数据成功
        fun onConversionDataSuccess(conversionDataMap: Map<String, Any>){}
        //转换数据失败时
        fun onConversionDataFail(errorMessage: String){}
        ////归因失败
        fun onAttributionFailure(errorMessage: String){}
    }

    private var listener: AfConversionListener? = null
    fun setAfConversionListener(listener: AfConversionListener?) {
        this.listener = listener
    }
}